//
//  SafeDetailViewController.m
//  Estate
//
//  Created by wangdongdong on 2017/10/25.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "SegmentViewController.h"
#import "SafeDetailBasicInformationVC.h"
#import "SafeDetailThreeTypePeopleVC.h"
#import "SafeDetailConstructDeviceVC.h"
#import "SafeDetailDangerViewController.h"
#import "SafeDetailPatrolViewController.h"
#import "SafeDetailSpecialTypeVC.h"
#import <MJExtension.h>

#import "SafeDetailViewController.h"


@interface SafeDetailViewController ()


@end


@implementation SafeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"安全";
    self.view.backgroundColor = UIColorFromRGB(0xf2f2f2);
    [self setProjectInformation];

    if (!self.isDiary) {
        // 自己的安全
        if ([USERID isEqualToString:self.safeModel.recordUser]) {
            // 创建提交
            if ([self.safeModel.recordType isEqualToString:@"SAFE_RECORD_01"] || [self.safeModel.state isEqualToString:@"0"]) {
                
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(prompting)];
            }
            
            // 创建暂存按钮
            if (![self.safeModel.recordType isEqualToString:@"SAFE_RECORD_01"] && [self.safeModel.state isEqualToString:@"0"] ) {
                
                [self createUI];
            }
            
            // 监理的安全(总监可编辑除了监理安全巡查)
        } else {
            // 创建提交
            if (![self.safeModel.recordType isEqualToString:@"SAFE_RECORD_01"] && ![self.safeModel.recifyState isEqualToString:@"1"]) {
                
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(prompting)];
            }
        }
    }
}

#pragma MARK - 提交
- (void)prompting {
    WDDAlertController *alertVC = [WDDAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [LSUtility pushWDDAlertVC:alertVC withTitle:@"提示" andMessageStr:@"提交后此条记录不可修改, 是否确认提交?"];
    
    // 确认
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self saveData];
    }];
    [alertVC addAction:sure];
    
    // 取消
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:cancel];
    [self presentViewController:alertVC animated:YES completion:nil];
}

// 提交
- (void)saveData {
    // 传参
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    // 保存类型:提交
    [parameDic setObject:@"1" forKey:@"saveType"];
    // 审阅状态
    NSString *role = DEF_PERSISTENT_GET_OBJECT(@"role");
    if ([role isEqual:@"USER_01"]) {
        [parameDic setObject:@"1" forKey:@"recifyState"];

    } else {
        [parameDic setObject:@"0" forKey:@"recifyState"];
    }
    // 通知(提交基本信息)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSafeBase" object:parameDic];
}

// 创建暂存按钮
- (void)createUI {
    // 暂存
    UIButton *submitBtn = [[UIButton alloc] init];
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-15);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.offset(190);
        make.height.offset(44);
    }];
    
    submitBtn.backgroundColor = UIColorFromRGB(0x25a498);
    submitBtn.layer.cornerRadius = 5;
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn setTitle:@"暂存" forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
}

// 暂存
- (void)submit {
    // 传参
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    // 保存类型:暂存
    [parameDic setObject:@"0" forKey:@"saveType"];
    // 审阅状态
    NSString *role = DEF_PERSISTENT_GET_OBJECT(@"role");
    if ([role isEqual:@"USER_01"]) {
        [parameDic setObject:@"1" forKey:@"recifyState"];
        
    } else {
        [parameDic setObject:@"0" forKey:@"recifyState"];
    }
    // 通知(暂存基本信息)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSafeBase" object:parameDic];
}

// 项目信息
- (void)setProjectInformation {
    SegmentViewController *segmentViewVC = [[SegmentViewController alloc] init];
    segmentViewVC.height = DEF_SCREEN_HEIGHT;
    // 名称数组
    NSMutableArray *mTitleArr = [NSMutableArray array];
    // 子视图数组
    NSMutableArray *controlArray = [NSMutableArray array];
    
    // 基本信息
    SafeDetailBasicInformationVC *baseVC = [[SafeDetailBasicInformationVC alloc] init];
    baseVC.isDiary = self.isDiary;
    baseVC.idStr = self.safeModel.id;
    baseVC.typeStr = self.safeModel.recordType;
    [mTitleArr addObject:@"基本信息"];
    [controlArray addObject:baseVC];
    
    // 检查信息
    // 安全施工三类人员
    if ([self.safeModel.recordType isEqualToString:@"SAFE_RECORD_02"]) {
        SafeDetailThreeTypePeopleVC *checkVC = [[SafeDetailThreeTypePeopleVC alloc] init];
        checkVC.isDiary = self.isDiary;
        checkVC.safeModel = self.safeModel;
        
        [controlArray addObject:checkVC];
        // 特种作业人员持证情况
    } else if ([self.safeModel.recordType isEqualToString:@"SAFE_RECORD_04"]) {
        SafeDetailSpecialTypeVC *checkVC = [[SafeDetailSpecialTypeVC alloc] init];
        checkVC.isDiary = self.isDiary;
        checkVC.safeModel = self.safeModel;
        [controlArray addObject:checkVC];
        
        // 监理安全巡查
    } else if ([self.safeModel.recordType isEqualToString:@"SAFE_RECORD_01"]) {
        SafeDetailPatrolViewController *checkVC = [[SafeDetailPatrolViewController alloc] init];
        checkVC.isDiary = self.isDiary;
        checkVC.safeModel = self.safeModel;
        [controlArray addObject:checkVC];
        
        // 危险性较大工程监理检查
    } else if ([self.safeModel.recordType isEqualToString:@"SAFE_RECORD_03"]) {
        SafeDetailDangerViewController *checkVC = [[SafeDetailDangerViewController alloc] init];
        checkVC.isDiary = self.isDiary;
        checkVC.safeModel = self.safeModel;
        [controlArray addObject:checkVC];

        // 施工设备进场验收监理核查
    } else if ([self.safeModel.recordType isEqualToString:@"SAFE_RECORD_05"]) {
        SafeDetailConstructDeviceVC *checkVC = [[SafeDetailConstructDeviceVC alloc] init];
        checkVC.isDiary = self.isDiary;
        checkVC.safeModel = self.safeModel;
        [controlArray addObject:checkVC];
    }
    [mTitleArr addObject:@"检查信息"];
    
    // 设置名称
    segmentViewVC.titleArray = mTitleArr;
    //设置子视图
    segmentViewVC.subViewControllers = controlArray;
    //设置选中字体颜色
    segmentViewVC.titleSelectedColor = [TTTheme defaultManger].defaultColor;
    // 标签栏背景色
    segmentViewVC.headViewBackgroundColor = [UIColor whiteColor];
    // 非选中状态下标签字体大小
    segmentViewVC.fontSize = 14.;
    // 选中下划线颜色
    segmentViewVC.bottomLineColor = [TTTheme defaultManger].defaultColor;
    
    segmentViewVC.buttonWidth = DEF_SCREEN_WIDTH / controlArray.count;
    segmentViewVC.buttonHeight = 44;
    [segmentViewVC initSegment];
    [segmentViewVC addParentController:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

